package com.ascent.client.ui;

import com.ascent.bean.User;
import com.ascent.client.util.UserDataClient;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;


/**
 *
 */
@SuppressWarnings("serial")
public class UserPanel extends JPanel {

    protected JList<Object> userListBox;

    protected JScrollPane userScrollPane;

    protected JButton addButton;
    protected JButton updateButton;

    protected JButton deleteButton;

    protected JButton exitButton;

    protected JPanel bottomPanel;

    protected MainFrame parentFrame;

    protected ArrayList<User> userArrayList;

    protected UserDataClient myDataClient;

    public UserPanel(MainFrame theParentFrame) {
        try {
            parentFrame = theParentFrame;
            myDataClient = new UserDataClient();

            userListBox = new JList<>();
            userListBox.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            userScrollPane = new JScrollPane(userListBox);

            updateList();

            addButton = new JButton("添加用户");
            deleteButton = new JButton("删除");
            exitButton = new JButton("退出");
            updateButton = new JButton("修改");

            bottomPanel = new JPanel();

            this.setLayout(new BorderLayout());

            this.add(BorderLayout.CENTER, userScrollPane);

            bottomPanel.setLayout(new FlowLayout());
            bottomPanel.add(addButton);
            bottomPanel.add(updateButton);
            bottomPanel.add(deleteButton);
            bottomPanel.add(exitButton);

            this.add(BorderLayout.SOUTH, bottomPanel);

//            clearButton.addActionListener(new ProductPanel.ClearActionListener());
            addButton.addActionListener(new AddActionListener());
            deleteButton.addActionListener(new DeleteActionListener());
            exitButton.addActionListener(new ExitActionListener());
            userListBox.addListSelectionListener(new UserListSelectionListener());
            updateButton.addActionListener(new UpdateActionListener());

            deleteButton.setEnabled(false);
            updateButton.setEnabled(false);
        } catch (IOException exc) {
            JOptionPane.showMessageDialog(this, "网络问题 " + exc, "网络问题", JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }
    }

    /**
     * 从服务器更新列表信息
     */
    private void updateList() {
        HashMap<String, User> users = myDataClient.getUsers();
        userArrayList = new ArrayList<>(users.values());

        Object[] theData = userArrayList.toArray();

        userListBox.setListData(theData);
        userListBox.updateUI();
    }

    /**
     * 处理新增用户的事件触发器
     */
    class AddActionListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            AddUserFrame addUserFrame = new AddUserFrame();
            addUserFrame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosed(WindowEvent e) {
                    updateList();
                }
            });
            addUserFrame.setVisible(true);
        }
    }
    /**
     * 处理退出按钮的事件触发器
     */
    class ExitActionListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            parentFrame.exit();
        }
    }
    /**
     * 处理更新按钮的事件触发器
     */
    class UpdateActionListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                int index = userListBox.getSelectedIndex();
                User user =(User) userArrayList.get(index);
                myDataClient.updateUser(user);

                UpdateUserFrame updateUserFrame = new UpdateUserFrame(user);
                updateUserFrame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosed(WindowEvent e) {
                        updateList();
                    }
                });
                updateUserFrame.setVisible(true);

            } catch (Exception ex) {
                ex.printStackTrace();
            }

        }
    }
    /**
     * 处理删除按钮的事件触发器
     */
    class DeleteActionListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                ConfirmFrame confirmFrame = new ConfirmFrame("删除用户");
                confirmFrame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosed(WindowEvent e) {
                        if (confirmFrame.isTrue()){
                            int index = userListBox.getSelectedIndex();
                            User user =(User) userArrayList.get(index);
                            myDataClient.deleteUser(user);
                            updateList();
                        }
                    }
                });
                confirmFrame.setVisible(true);

            } catch (Exception ex) {
                ex.printStackTrace();
            }

        }
    }
    /**
     * 处理选中分类列表中选项时触发的事件监听器
     */
    class UserListSelectionListener implements ListSelectionListener {
        public void valueChanged(ListSelectionEvent event) {
            if (userListBox.isSelectionEmpty()) {
                deleteButton.setEnabled(false);
                updateButton.setEnabled(false);
            } else {
                deleteButton.setEnabled(true);
                updateButton.setEnabled(true);
            }
        }
    }


}
